# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Albert Graef <aggraef at gmail.com>
# Contributor: Bernardo Barros <bernardobarros at gmail dot com>
# Contributor: bjoern lindig (bjoern _dot_ lindig _at_ google.com)

pkgname=faust
pkgver=2.54.9
pkgrel=3
pkgdesc="A functional programming language for realtime audio signal processing."
arch=(loong64 x86_64)
url="https://faust.grame.fr/"
license=(GPL2)
groups=(pro-audio)
depends=(
  gcc-libs
  glibc
  libmicrohttpd
  llvm-libs
)
makedepends=(
  cmake
  libsndfile
  llvm
  xxd
)
optdepends=(
  'clang: for sound2reader'
  'csound: for faust2csound'
  'gradle: for faust2android'
  'graphviz: for faust2sig{,viewer}'
  'gtk2: for faust2{jack,rosgtk}'
  'inkscape: for faust2{pdf,png}'
  'jack: for faust2{api,jack{,console,internal,server,rust},jaqt,netjackqt,nodejs,ros,rosgtk,rpinetjackconsole}'
  'swig: for faust2android'
  'libsndfile: for faust2{dummy,sndfile}'
  'octave: for faust2octave'
  'pd: for faust2puredata'
  'portaudio: for faust2paqt'
  'python: for faust2atomsnippets, faust2md and faust2sc.py'
  'qt5-tools: for faust2{faustvst,jaqt,lv2}'
  'rtaudio: for faust2raqt'
  'ruby-rexml: for faust2sc and scbuilder'
  'rust: for faust2jackrust'
  'supercollider: for faust2supercollider'
  'texlive-core: for faust2pdf'
)
provides=(
  libfaustmachine.so
  libfaust.so
  libOSCFaust.so
  libHTTPDFaust.so
)
options=(staticlibs)
source=(
  https://github.com/grame-cncm/$pkgname/releases/download/$pkgver/$pkgname-$pkgver.tar.gz
  $pkgname-2.54.9-sound2faust_makefile.patch
  $pkgname-2.54.9-missing_header.patch::https://github.com/grame-cncm/faust/commit/2560b69377edf0664a8338b2c75f70032a48f054.patch
)
sha512sums=('cc3fef369cb9820c341185e1f8f9c0503cdd41fcc2b3bb4d7b0d51c7bbb65857caf089127ac489113ce8cbe7226bcfd35eac8b2d4d6eaeb2dfd0fb21df053660'
            '7bc75d5d9ab826fcdecffc054e0aaa9e74a3abce3a1c89a306a5c3910b847878fd9946aa230067de31bd4a7ba377f08165d2969589fbaa28d9b9a07d1af144ea'
            'ea176e0ff5cc4ec7dd83c49c59f9e890bbc14ade454a103e3f96a80059222ea150e36dc5d8408ccb5a2d37d6b396bdc729cf326dc111a0331aef395eb902e4aa')
b2sums=('6ba6bd36c7c2561fb520cad0e7f9fb8f1261c934defe7146d7bd773df9ec4eb14d1615fae40326b6e2c6b4bea6e75634e1eba687267f873383530af5fd291bb6'
        'dabdc6839a9adc5a559ddb0645fafb5bd00abe225bd5ca8a9b41c57b03d63ae957e09d20244ea2c19770c74141a847c66de28d9cab6a7d0e5f4ce70ef6fe9f05'
        'e9e105ffd54b34b0b896c7d736b5a6d3b8c530087e0b6b945abc57cffc89e1080d493d890bf0e73db68f4b816ecef12d0521de2c8210676a95624e9d9e6e23ab')

prepare() {
  # do not link sound2faust against libsndfile.a: https://github.com/grame-cncm/faust/issues/840
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.54.9-sound2faust_makefile.patch
  # add missing header: https://bugs.archlinux.org/task/78405
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.54.9-missing_header.patch
}

build() {
  local cmake_options=(
        -B build
        -C $pkgname-$pkgver/build/backends/all.cmake
        -C $pkgname-$pkgver/build/targets/all.cmake
        -D CMAKE_INSTALL_PREFIX=/usr
        -D CMAKE_BUILD_TYPE=None
        -D INCLUDE_DYNAMIC=ON
        -D INCLUDE_STATIC=ON
        -D INCLUDE_ITP=ON
        -W no-dev
        -S $pkgname-$pkgver/build
  )
  # prevent static libs from being mangled with LTO
  CXXFLAGS+=' -ffat-lto-objects'

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
  make VERBOSE=1 -C $pkgname-$pkgver/tools/sound2faust
}

check() {
  ctest --test-dir build --output-on-failure
}

package() {
  DESTDIR="$pkgdir" cmake --install build
  cd $pkgname-$pkgver
  make VERBOSE=1 PREFIX=/usr DESTDIR="$pkgdir" install -C tools/sound2faust

  # docs
  install -vDm 644 documentation/{,misc/}*.pdf -t "$pkgdir/usr/share/doc/$pkgname"

  # examples
  install -vd "$pkgdir/usr/share/$pkgname/examples"
  cp -vR "examples/"* "$pkgdir/usr/share/$pkgname/examples/"

  cd syntax-highlighting
  # atom
  install -vDm 644 atom/language-$pkgname/package.json -t "$pkgdir/usr/lib/atom/dot-atom/packages/language-$pkgname/"
  install -vDm 644 atom/language-$pkgname/grammars/$pkgname.cson -t "$pkgdir/usr/lib/atom/dot-atom/packages/language-$pkgname/grammars/"
  install -vDm 644 atom/language-$pkgname/settings/language-$pkgname.cson -t "$pkgdir/usr/lib/atom/dot-atom/packages/language-$pkgname/settings/"
  install -vDm 644 atom/language-$pkgname/snippets/* -t "$pkgdir/usr/lib/atom/dot-atom/packages/language-$pkgname/snippets/"
  install -vDm 644 atom/language-$pkgname/process-palette.json.linux "$pkgdir/usr/share/doc/$pkgname/process-palette.json"
  # kate
  install -vDm 644 $pkgname.xml -t "$pkgdir/usr/share/apps/katepart/syntax/"
  # gedit
  install -vDm 644 $pkgname.lang -t "$pkgdir/usr/share/gtksourceview-2.0/language-specs/"
  install -vDm 644 $pkgname.lang -t "$pkgdir/usr/share/gtksourceview-3.0/language-specs/"
  # highlight
  install -vDm 644 dsp.lang -t "$pkgdir/usr/share/highlight/langDefs/"
  # nano
  install -vDm 644 $pkgname.nanorc -t "$pkgdir/usr/share/nano/"
  # vim
  install -vDm 644 $pkgname.vim -t "$pkgdir/usr/share/vim/vimfiles/syntax/"
  # emacs
  install -vDm 644 $pkgname-mode.el -t "$pkgdir/usr/share/emacs/site-lisp/"

  # remove unusable scripts
  rm -v "$pkgdir/usr/bin/faust2au"
  # remove precompiled shared libraries for android:
  # https://github.com/grame-cncm/faust/issues/370
  rm -rvf "$pkgdir/usr/share/faust/android/app/"{lib,oboe} "$pkgdir/usr/share/faust/smartKeyboard/android/app/oboe"
}

